<?php

namespace app\common\traits;

use think\response\Json;
use app\common\model\Region;

trait RegionTrait
{
    protected Region $region;
    protected int    $parent_id;
    protected string $keywords;

    public function initialize(): void
    {
        parent::initialize();
        $get             = $this->request->get();
        $this->parent_id = $get['parent_id'] ?? 0;
        $this->keywords  = $get['keywords'] ?? '';
        $this->region    = new Region();
    }

    /**
     * 获取地区
     * @return Json
     */
    public function getRegion(): Json
    {
        return $this->success($this->region->getRegion($this->parent_id));
    }

    /**
     * 获取省份
     * @return Json
     */
    public function getProvince(): Json
    {
        return $this->success($this->region->getProvince());
    }

    /**
     * 获取城市
     * @return Json
     */
    public function getCity(): Json
    {
        return $this->success($this->region->getCity($this->parent_id));
    }

    /**
     * 获取区县
     * @return Json
     */
    public function getDistrict(): Json
    {
        return $this->success($this->region->getDistrict($this->parent_id));
    }

    /**
     * 获取街道
     * @return Json
     */
    public function getStreet(): Json
    {
        return $this->success($this->region->getStreet($this->parent_id));
    }

    /**
     * 搜索地区
     * @return Json
     */
    public function searchRegion(): Json
    {
        $keywords = $keywords ?? 'bj';
        return $this->success($this->region->searchRegion($keywords, $this->parent_id));
    }

    /**
     * 搜索省份
     * @return Json
     */
    public function searchProvince(): Json
    {
        $keywords = $keywords ?? 'hn';
        return $this->success($this->region->searchProvince($keywords));
    }

    /**
     * 搜索城市
     * @return Json
     */
    public function searchCity(): Json
    {
        $keywords = $keywords ?? 'ny';
        return $this->success($this->region->searchCity($keywords, $this->parent_id));
    }

    /**
     * 搜索区县
     * @return Json
     */
    public function searchDistrict(): Json
    {
        $keywords = $keywords ?? 'lc';
        return $this->success($this->region->searchDistrict($keywords, $this->parent_id));
    }

    /**
     * 搜索街道
     * @return Json
     */
    public function searchStreet(): Json
    {
        $keywords = $keywords ?? 'bs';
        return $this->success($this->region->searchStreet($keywords, $this->parent_id));
    }
}